package org.larkdoc.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.larkdoc.constant.ViewConts;
import org.larkdoc.model.bo.GroupBO;
import org.larkdoc.model.entity.GroupInfo;
import org.larkdoc.model.entity.GroupRelUser;
import org.larkdoc.model.vo.GroupVO;
import org.larkdoc.model.vo.RespBaseVO;
import org.larkdoc.service.GroupService;
import org.larkdoc.util.AuthUtil;
import org.larkdoc.util.DozerUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 组信息控制类
 * 
 * @author  zhangpeijun[zhangpeijun1024@qq.com]
 * @version  [v1.0.1, 2017年10月17日]
 * @see  [相关类/方法]
 * @since  [产品/模块版本]
 */
@Controller
@RequestMapping("/group")
public class GroupController {
    
    private final static Logger log = LoggerFactory.getLogger(GroupController.class);
    
    @Autowired
    GroupService                groupService;
    
    @RequestMapping("/create")
    @ResponseBody
    public RespBaseVO create(GroupVO group) {
        RespBaseVO respVo = new RespBaseVO();
        if (null != group){
            group.setUpdateTime(new Date());
            group.setCreateTime(new Date());
            group.setCreator(AuthUtil.getAccount());
            if (groupService.insertSelective(groupVO2BO(group)) != 1) {
                respVo.setRetCode(ViewConts.Code.FAILURE);
                respVo.setRetMsg(ViewConts.Msg.FAILURE);
            }  
        }
        return respVo;
    }
    
    @RequestMapping("/delete/{id}")
    @ResponseBody
    public RespBaseVO delete(@PathVariable int id) {
        RespBaseVO respVo = new RespBaseVO(ViewConts.Code.FAILURE, ViewConts.Msg.FAILURE, null);
        if (id > 0) {
            GroupInfo groupInfo = groupService.selectByPrimaryKey(id);
            if (null != groupInfo && AuthUtil.getAccount().equals(groupInfo.getCreator())) {
                if (groupService.deleteByPrimaryKey(id) == 1) {
                    respVo.setRetCode(ViewConts.Code.SUCCESS);
                    respVo.setRetMsg(ViewConts.Msg.SUCCESS);
                }
            }
        }
        return respVo;
    }
    
    @RequestMapping("/get/{id}")
    @ResponseBody
    public RespBaseVO get(@PathVariable int id) {
        RespBaseVO respVo = new RespBaseVO();
        if (id > 0) {
            GroupInfo group = groupService.selectByPrimaryKey(id);
            if (null != group){
                GroupVO vo = DozerUtil.conversion(group, GroupVO.class);
                // TODO 待补充用户
                respVo.setRetVal(vo);
            }
        }
        return respVo;
    }
    
    @RequestMapping("/update")
    @ResponseBody
    public RespBaseVO update(GroupVO vo) {
        RespBaseVO respVo = new RespBaseVO(ViewConts.Code.FAILURE, ViewConts.Msg.FAILURE, null);
        if (null != vo) {
            GroupInfo groupDB = groupService.selectByPrimaryKey(vo.getId());
            // 用户只能更新自己创建的群。
            if (null != groupDB && AuthUtil.getAccount().equals(groupDB.getCreator())) {
                vo.setUpdateTime(new Date());
                vo.setCreateTime(null);
                vo.setCreator(AuthUtil.getAccount());
                if (groupService.insertSelective(groupVO2BO(vo)) == 1) {
                    respVo.setRetCode(ViewConts.Code.SUCCESS);
                    respVo.setRetMsg(ViewConts.Msg.SUCCESS);
                }
            } else {
                log.info("update fail, no permissions. param : {} , query : ", vo, groupDB);
            }
        }
        return respVo;
    }
    
    private GroupBO groupVO2BO(GroupVO vo) {
        GroupBO bo = null;
        if (null != vo) {
            vo.setCreator(AuthUtil.getAccount());
            bo = new GroupBO();
            bo.setGroupInfo(DozerUtil.conversion(vo, GroupInfo.class));
            if (StringUtils.isNotBlank(vo.getUserIds())) {
                String[] userIds = vo.getUserIds().split(",");
                List<GroupRelUser> users = new ArrayList<GroupRelUser>(userIds.length);
                GroupRelUser user = null;
                try {
                    for (String id : userIds) {
                        user = new GroupRelUser();
                        user.setUserId(Integer.valueOf(id));
                        users.add(user);
                    }
                } catch (NumberFormatException e) {
                    log.error("groupVO2BO NumberFormatException ", e);
                    log.error("groupVO2BO NumberFormatException ： {}", vo.getUserIds());
                }
                bo.setGroupRelUsers(users);
            }
        }
        return bo;
    }
}
